<?php
/**
 * 养宠推荐
 * Red 2012/1/31
*/
include_once './include/init.inc.php';

//记录测试人数
commendList($db);

$array = array();
$array[0] = '0';		    //适合导盲 拉布拉多。。。。。补充 金毛
$array[1] = '82,127';		//非常友善,喜欢孩子
$array[2] = '83';	        //善于看家
$array[3] = '79,82';		//喜欢黏人、非常友善、小型犬
$array[4] = '79,82';		//喜欢粘人、非常友善
$array[5] = '79,82,81';		//喜欢粘人、非常友善、容易训练

$array[6] = '99,75';		//耐寒,长毛
$array[7] = '99,75';		//耐寒,长毛
$array[8] = '84,76,78';		//耐热,短毛,容易打理
$array[9] = '0';		    //全部
$array[10] = '76,78';		//短毛,容易打理
$array[11] = '76,84,78';	//短毛,耐热,容易打理

$array[12] = '66';		    //小型犬
$array[13] = '66,65';		//小型犬,中型犬
$array[14] = '66,65';		//小型犬,中型犬
$array[15] = '0';		    //全部
$array[16] = '0';		    //全部
$array[17] = '66,65';		//小型犬,中型犬

$array[18] = '102,82,77';   //不爱叫,非常友善,无体味
$array[19] = '131';		    //不适合爬楼梯
$array[20] = '102,82,77';   //不爱叫,非常友善,无体味
$array[21] = '131';		    //不适合爬楼梯
$array[22] = '0';		    //全部
$array[23] = '0';		//全部

$array[24] = '102,82,83';   //不爱叫,非常友善,善于看家
$array[25] = '102,82,127';	//不爱叫,非常友善,喜欢小孩
$array[26] = '102';         //不爱叫
$array[27] = '102';		    //不爱叫
$array[28] = '102,81,82';	//不爱叫 ,容易训练,非常友善
$array[29] = '128,82';		    //善待其他动物,非常友善

$array[30] = '0';           //全部
$array[31] = '129';	        //大运动量
$array[32] = '129';          //大运动量
$array[33] = '82';		    //非常友善
$array[34] = '0';	        //全部
$array[35] = '0';		    //全部

$array[36] = '0';           //全部
$array[37] = '0';	        //全部
$array[38] = '0';           //全部
$array[39] = '74';		    //不掉毛
$array[40] = '74,76,77';	//不掉毛,短毛,无体味
$array[41] = '74,76,77';		    //不掉毛,短毛,无体味

$array[42] = '0';           //全部
$array[43] = '0';	        //全部
$array[44] = '76';           //短毛
$array[45] = '76,78';		    //短毛、容易打理
$array[46] = '74,76,78';	    //不掉毛、短毛、容易打理
$array[47] = '74,76,77,78';	//不掉毛、短毛、无体味、容易打理

$array[48] = '132';           //气质沉稳
$array[49] = '133';	        //气质随性
$array[50] = '129';           //运动量大
$array[51] = '134';		    //毛色丰富
$array[52] = '135';	    //气质威严
$array[53] = '136';		//气质华美

$array[54] = '64,75';           //大型犬 长毛
$array[55] = '64,76';	        //大型犬 短毛
$array[56] = '65';           //中型犬
$array[57] = '69';		    //梗犬组
$array[58] = '66,102';	    //小型犬,不爱叫
$array[59] = '66';		//小型犬,爱叫


//$s= '0|6|16|22|27|30|38|44|48|59|';
$s = $_REQUEST['s'];
$s = substr($s, 0, -1);
$arr_s = explode('|',$s);

if(count($arr_s) != 10){
	header('Location: /commend.php');
	exit();
}

$ar = array();	//$ar 为10个选项下的10个宠物ID数组
foreach ($arr_s as $key => $val ){
	$sql = "select goods_id from `info_goods_cat` where cat_id in (".$array[$val].")";
	$ar_temp = $db->fetch_all($sql);
	$ar = redArrIn($ar,$ar_temp);
}
//print_r($ar);echo count($ar);echo "</br>=================================================</br>";

//统计各个ID出现的次数
$result = array_count_values($ar);

//按倒序排序
arsort($result);
//print_r($result);

$re = array();
foreach($result as $key=>$val){
	$re[] = $key;
}
//print_r($re);

if($arr_s[0]=='0'){
	$re_six = daomang($re);	//选定导盲犬后的排序变化
}else{
	$re_six = array_slice($re, 0, 40);	//防止出现猫  cat_id in ('2','64','65','66')
}

//冷门犬id
$del_goods_id = '2016,1831,149,147,144,143,142,141,135,123,121,120,115,114,107,106,104,95,84,83,81,80,79,73,72,69,68,67,66,63,62,61,59,57,55,52,51,49,48,46,45,44,42,41,40,39,38,35,34,33,30,27,26,25,24,23,22,21,20,19,18,17,16,15,14,12,11,10,6,5,2';

$goods_id = implode(',',$re_six);
$dog_all = $db->fetch_all("select * from `info_goods` WHERE goods_id  in (".$goods_id.") and cat_id in ('64','65','66') and goods_id not in (".$del_goods_id.")");
$dog_all = array_slice($dog_all, 0, 6);
$dog_all = newdoglist($dog_all,$re_six);	//按照权重排序
$dogs = array();
$type = array('64'=>'大型犬','65'=>'中型犬','66'=>'小型犬');
$i=1;
foreach($dog_all as $key=>$val){
	$attr = array();
	$attr = get_goods_properties($val['goods_id'],$db); //属性
	$val['attr'] = @array_reverse($attr['pro']['商品属性']);
	$val['attr'] = @array_slice($val['attr'], 1, 2);
	$val['type'] = $type[$val['cat_id']];
	$val['num'] = $i;
	$dogs[] = $val;
	++$i;
}
$smarty->assign('dogs',       $dogs);

$smarty->assign('nav','commend');
fheader('commend/commend_end.css');
$smarty->display('commend/commend_end.dwt');
ffooter();

function redArrIn($ar=array(),$ar_temp=array()){
	foreach($ar_temp as $key=>$val){
		if(!empty($val) && $val['goods_id']>0){
			array_push($ar,$val['goods_id']);
		}
	}
	Return $ar;
}

function odd($val){
	$daomang = array('37','85','87','97','146');
	Return in_array($val,$daomang);
}
function daomang($re=array()){
	$daomang = array('37','85','87','97','146');		//拉布拉多,卡南犬,德国牧羊犬,拳师,贵宾犬
	$re = array_filter($re, "odd");
	foreach($daomang as $key=>$val){
		if(!in_array($val,$re)){
			$re[] = $val;
		}
	}
	$re[] = '36';	//补充 金毛
	Return $re;
}

//按照先前顺序重新排列
function newdoglist($old_arr,$arr){
	$new_arr = array();
	$i = 0;
	foreach($arr as $key=>$val){
		foreach($old_arr as $key2=>$val2){
			if($val2['goods_id']==$val){
				$new_arr[] = $val2;
			}
		}
	}
	Return $new_arr;
}

function commendList($db){
	$ip = getIP2();
	$dateline = time();
	$oldtime = $dateline - 60; //一分钟前
	$re = $db->fetch_one("select id from commend_list WHERE ip='$ip' and dateline>'$oldtime'");
	if(empty($re)){	//可以记录。注：同一ip，不能在一分钟内记录测试数。
		$db->query("insert into commend_list(`ip`,`dateline`) values('$ip','$dateline')");
		$db->query("update commend set num=num+1");
	}
}

function getIP2() {
	if (getenv ( "HTTP_CLIENT_IP" ) && strcasecmp ( getenv ( "HTTP_CLIENT_IP" ), "unknown" )) {
		$ip = getenv ( "HTTP_CLIENT_IP" );
	} else if (getenv ( "HTTP_X_FORWARDED_FOR" ) && strcasecmp ( getenv ( "HTTP_X_FORWARDED_FOR" ), "unknown" )) {
		$ip = getenv ( "HTTP_X_FORWARDED_FOR" );
	} else if (getenv ( "REMOTE_ADDR" ) && strcasecmp ( getenv ( "REMOTE_ADDR" ), "unknown" )) {
		$ip = getenv ( "REMOTE_ADDR" );
	} else if (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], "unknown" )) {
		$ip = $_SERVER ['REMOTE_ADDR'];
	} else {
		$ip = "unknown";
	}

	return ( $ip == 'unknown' || ip2long ( $ip ) === false || ip2long ( $ip ) == -1 ) ? '0.0.0.0' : $ip;
}

?>